package org.nci.platform.workflow.expand.system.service.impl;

import cn.hutool.core.lang.func.LambdaUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.alibaba.fastjson2.JSON;
import org.nci.platform.workflow.expand.system.entity.WorkflowDept;
import org.nci.platform.workflow.expand.system.service.WorkflowDeptService;

import java.util.List;

public class DemoDeptImpl implements WorkflowDeptService {
    String data = "[" +
            "{\"deptId\":\"dept_200\" ,\"parentId\":\"dept_root\",\"deptCode\":\"200\",\"deptName\":\"酒店\"}," +
            "{\"deptId\":\"dept_2001\",\"parentId\":\"dept_200\",\"deptCode\":\"200100\",\"deptName\":\"前厅部\"}," +
            "{\"deptId\":\"dept_2002\",\"parentId\":\"dept_200\",\"deptCode\":\"200200\",\"deptName\":\"客房部\"}," +
            "{\"deptId\":\"dept_2004\",\"parentId\":\"dept_200\",\"deptCode\":\"200300\",\"deptName\":\"财务部\"}," +
            "{\"deptId\":\"dept_2005\",\"parentId\":\"dept_200\",\"deptCode\":\"200400\",\"deptName\":\"行政部\"}," +
            "{\"deptId\":\"dept_2003\",\"parentId\":\"dept_200\",\"deptCode\":\"200500\",\"deptName\":\"餐饮部\"}" +
            "]";


    @Override
    public List<Tree<String>> deptTree() {
        List<WorkflowDept> workflowDepts = JSON.parseArray(data, WorkflowDept.class);
        return TreeUtil.build(workflowDepts, "dept_root", (workflowDept, tree) -> {
                    tree.setId(workflowDept.getDeptId());
                    tree.setParentId(workflowDept.getParentId());
                    tree.put(LambdaUtil.getFieldName(WorkflowDept::getDeptCode), workflowDept.getDeptCode());
                    tree.setName(workflowDept.getDeptName());
                }
        );

    }
}
